<template>
  <div class="app-container">
    <!-- 月度总结 -->
    <!-- <div style="display: flex">
      <p style="margin-top: 25px; margin-left: 20px">展示列：</p>
      <el-checkbox-group v-model="monthCheckList" style="margin-top: 20px">
        <el-checkbox label="职位" value="1" />
        <el-checkbox label="出勤天数" value="2" />
        <el-checkbox label="休息天数" value="3" />
        <el-checkbox label="工作时长" value="4" />
        <el-checkbox label="迟到次数" value="5" />
        <el-checkbox label="迟到时长" value="6" />
        <el-checkbox label="旷工迟到天数" value="7" />
        <el-checkbox label="早退次数" value="8" />
        <el-checkbox label="早退时长" value="9" />
        <el-checkbox label="上班缺卡次数" value="10" />
        <el-checkbox label="下班缺卡次数" value="11" />
        <el-checkbox label="旷工天数" value="12" />
        <el-checkbox label="加班时长（转调休）" value="13" />
      </el-checkbox-group>
    </div> -->
    <div
      style="
        display: flex;
        align-items: center;
        justify-content: space-between;
        margin-top: 20px;
      "
    >
      <div style="display: flex">
        <div>
          <el-date-picker
            v-model="timeValue"
            type="daterange"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="YYYY-MM-DD"
            style="width: 300px; margin: 0 12px 10px 0"
            @change="handleTime"
          />
        </div>

        <div
          style="
            width: 210px;
            display: flex;
            align-items: center;
            justify-content: left;
            font-size: 14px;
            margin-top: -10px;
          "
        >
          <span>部门/人员:</span>
          <select-show
            v-model:orgList="deptOrUserList"
            type="org"
            :disable-select-children-dept="false"
            :multiple="true"
            :showStyle="3"
          ></select-show>
        </div>

        <el-checkbox
          v-model="containCheck"
          style="margin-left: 20px"
          @change="handleContain"
        >
          离职员工
        </el-checkbox>
        <span
          style="
            font-size: 14px;
            color: #777;
            margin-left: -10px;
            margin-top: 5px;
          "
          >(保留三个月)</span
        >
      </div>
      <!-- 导出按钮 -->
      <el-row>
        <el-col>
          <el-button
            type="primary"
            plain
            icon="Download"
            v-hasPermi="['attendance:statistics:export']"
            @click="handleExport"
            >导出</el-button
          >
        </el-col>
      </el-row>
    </div>
    <el-table v-loading="loading" :data="postList">
      <el-table-column label="姓名" align="center" prop="userName" />
      <el-table-column label="考勤组" align="center" prop="groupName" />
      <el-table-column label="部门" align="center" prop="dept" />
      <el-table-column label="工号" align="center" prop="jobId" />
      <el-table-column label="职位" align="center" prop="postName" />
      <el-table-column label="出勤天数" align="center" prop="attendDayNumber" />
      <el-table-column label="出勤班次" align="center" prop="attendShift" />
      <el-table-column label="休息天数" align="center" prop="restDays" />
      <el-table-column label="工作时长" align="center" prop="workHours" />
      <el-table-column label="迟到次数" align="center" prop="lateNumber" />
      <el-table-column label="迟到时长" align="center" prop="lateHours" />
      <el-table-column
        label="严重迟到次数"
        align="center"
        prop="seriousLateNumber"
      />
      <el-table-column
        label="严重迟到时长(分钟)"
        align="center"
        prop="seriousLateHours"
      />
      <el-table-column label="旷工迟到天数" align="center" prop="lateDays" />
      <el-table-column
        label="早退次数"
        align="center"
        prop="leaveEarlyNumber"
      />
      <el-table-column label="早退时长" align="center" prop="leaveEarlyHours" />
      <el-table-column label="缺卡次数" align="center" prop="notclockNumber" />
      <el-table-column label="旷工天数" align="center" prop="absentDays" />
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script setup name="Monthly">
import { getMonthlyListApi } from "@/api/attendance/statistics";
import selectShow from "@/views/flyflow/components/orgselect/selectAndShow.vue";
import moment from "moment";

const { proxy } = getCurrentInstance();
const postList = ref([]);
const loading = ref(true);
const total = ref(0);
const timeValue = ref([
  moment().startOf("month").format("YYYY-MM-DD"),
  moment().format("YYYY-MM-DD"),
]);
const deptOrUserList = ref([]);
const containCheck = ref(true);

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 1,
    startDay: moment().startOf("month").format("YYYY-MM-DD"), //开始日期
    endDay: moment().format("YYYY-MM-DD"), //结束日期
    deptIds: [], //部门集合
    userIds: [], //员工集合
    contain: 2, //是否包含90天离职人员 1:不包含 2:包含
  },
});

const { queryParams } = toRefs(data);

/**
 * 监听部门人员查询
 */
watch(
  () => deptOrUserList.value,
  (val) => {
    val.map((item) => {
      if (item.type == "user") {
        queryParams.value.userIds.push(item.id);
      } else if (item.type == "dept") {
        queryParams.value.deptIds.push(item.id);
      }
    });

    getList();
  },
  { deep: true }
);

/** 查询月度汇总列表 */
function getList() {
  loading.value = true;
  getMonthlyListApi(queryParams.value).then((response) => {
    postList.value = response.rows;
    total.value = Number(response.total);
    loading.value = false;
  });
}

/**
 * 处理时间查询
 */
const handleTime = (val) => {
  queryParams.value.startDay = val[0];
  queryParams.value.endDay = val[1];
  getList();
};

/**
 * 处理是否90天内离职人员查询
 */
const handleContain = () => {
  containCheck.value == true
    ? (queryParams.value.contain = 2)
    : (queryParams.value.contain = 1);
  getList();
};

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "attendance/statistics/monthly/export",
    {
      ...queryParams.value,
    },
    `月度汇总_${timeValue.value[0]}到${timeValue.value[1]}.xlsx`
  );
}

getList();
</script>
<style scoped>
:deep(.el-checkbox) {
  margin-right: 20px;
}
</style>
